﻿Imports System.Collections.ObjectModel
Imports FL_8x0.Common
Imports FL_8x0.FL_830.Model
Imports FL_8x0.Model
Imports GalaSoft.MvvmLight
Imports GalaSoft.MvvmLight.CommandWpf

Namespace FL_8x0.Viewmodel

    Public Class ReportViewModel
        Inherits ViewModelBase

        Private _idList As ObservableCollection(Of String)            '产品Id列表
        Private _fittingModeList As ObservableCollection(Of String)   'Fitting Mode模式列表
        Private _id As String                                         '当前product ID
        Private _productCode As String                                '当前product code
        Private _productModel As ProductModel                         '产品（组参数）

        Private _gpUnit As String                                     '直压单位
        Private _leakUnit As String                                   '小漏单位

        Private _applyCommand As RelayCommand(Of Window)               '点击“确认”触发命令
        Private _cancelCommand As RelayCommand(Of Window)              '点击“取消”触发命令

        Private _reportModel As ReportModel                            '报告

        Public Sub New()
            Dim ids = User_ListProductAndName()
            IdList = New ObservableCollection(Of String)(ids)
            FittingModeList = New ObservableCollection(Of String)(New List(Of String)(g_strFittingMode))
            If g_udtProduct.Id <> "" Then
                Id = g_udtProduct.Id & "  /  " & g_udtProduct.Name
            Else
                Id = ids(0)
            End If
            FillEditModel(Id)
            ReportModel = New ReportModel
            ApplyCommand = New RelayCommand(Of Window)(Sub(window) Apply(window))
            CancelCommand = New RelayCommand(Of Window)(Sub(window) Cancel(window))
        End Sub

        '=====================================================================
        '函 数 名: Apply
        '参    数: 
        '返 回 值:
        '功能描述: 报告存库，记录全局变量
        '=====================================================================
        Public Sub Apply(ByVal window As Window)
            ReportModel.ReportId = SystemData.idWorker.nextId()
            If ReportModel.ProductCode = "" Then
                ReportModel.ProductCode = "空"
            End If
            User_AddReport(ReportModel)
            g_udtReport = ReportModel
            g_udtProduct = ProductModel
            window.DialogResult = True
        End Sub

        '=====================================================================
        '函 数 名: Cancel
        '参    数: 
        '返 回 值:
        '功能描述: 取消
        '=====================================================================
        Private Sub Cancel(ByVal window As Window)
            If IsNothing(g_udtReport.ReportId) Then
                g_udtReport.ReportId = ""
            End If
            ProductCode = ""
            window.DialogResult = False
        End Sub

        '=====================================================================
        '函 数 名: FillEditModel
        '参    数: strId 产品Id号
        '返 回 值:
        '功能描述: 读取数据库数据填充ProductModel
        '=====================================================================
        Private Sub FillEditModel(ByVal id As String)
            Dim ids As String() = id.Split("  /  ")
            ProductModel = User_ReadProduct(ids(0))
            GpUnit = g_strGPUnit(ProductModel.GpUnitIndex)
            If ProductModel.LeakUnitIndex = 1 Then
                LeakUnit = GpUnit
            Else
                LeakUnit = g_strLeakUnit(ProductModel.LeakUnitIndex)
            End If
        End Sub

        Public Property IdList As ObservableCollection(Of String)
            Get
                Return _idList
            End Get
            Set
                _idList = Value
            End Set
        End Property

        Public Property Id As String
            Get
                Return _id
            End Get
            Set
                If _id <> Value Then
                    FillEditModel(Value)
                End If
                _id = Value
                RaisePropertyChanged()
            End Set
        End Property

        Public Property ProductModel As ProductModel
            Get
                Return _productModel
            End Get
            Set
                _productModel = Value
                RaisePropertyChanged()
            End Set
        End Property

        Public Property GpUnit As String
            Get
                Return _gpUnit
            End Get
            Set(value As String)
                _gpUnit = value
                RaisePropertyChanged()
            End Set
        End Property

        Public Property LeakUnit As String
            Get
                Return _leakUnit
            End Get
            Set(value As String)
                _leakUnit = value
                RaisePropertyChanged()
            End Set
        End Property

        Public Property CancelCommand As RelayCommand(Of Window)
            Get
                Return _cancelCommand
            End Get
            Set(value As RelayCommand(Of Window))
                _cancelCommand = value
            End Set
        End Property

        Public Property ApplyCommand As RelayCommand(Of Window)
            Get
                Return _applyCommand
            End Get
            Set(value As RelayCommand(Of Window))
                _applyCommand = value
            End Set
        End Property

        Public Property ReportModel As ReportModel
            Get
                Return _reportModel
            End Get
            Set(value As ReportModel)
                _reportModel = value
            End Set
        End Property

        Public Property ProductCode As String
            Get
                Return _productCode
            End Get
            Set
                _productCode = Value
                RaisePropertyChanged()
            End Set
        End Property

        Public Property FittingModeList As ObservableCollection(Of String)
            Get
                Return _fittingModeList
            End Get
            Set
                _fittingModeList = Value
            End Set
        End Property
    End Class
End Namespace